﻿using Dapper;
using Domain.Entitys;
using Domain.Repositorys;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class CFSKRepositoty : ICFSKRepositoty
    {
        private readonly MyDbContext myDbContext;

        public CFSKRepositoty(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        /// <summary>
        /// 删除2级结构表
        /// </summary>
        /// <param name="cFSK"></param>
        /// <returns></returns>
        public async Task<int> CFSKDeleteAsync(CFSK cFSK)
        {
            myDbContext.CFSKs.Remove(cFSK);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 添加二级结构表数据
        /// </summary>
        /// <param name="cFSK"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> CFSKInsertAsync(CFSK cFSK)
        {
            await myDbContext.CFSKs.AddAsync(cFSK);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 根据2级结构编号查询数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<CFSK> CFSKSelectByCfkid(string id)
        {
            return await myDbContext.CFSKs.FirstOrDefaultAsync(c => c.SecondKindId == id);
        }

        /// <summary>
        /// 根据id查询对应数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<CFSK> CFSKSelectById(int id)
        {
            return myDbContext.CFSKs.FirstOrDefaultAsync(c => c.FskId == id);
        }

        /// <summary>
        /// 分页查询2级结构表数据
        /// </summary>
        /// <param name="fenYe"></param>
        /// <returns></returns>
        public FenYe<CFSK> CFSKSelectFenYe(FenYe<CFSK> fenYe)
        {
            using (SqlConnection sc = new SqlConnection("Data Source=.;Initial Catalog=HR_DB;Integrated Security=True;Connect Timeout=30;Encrypt=False;"))
            {
                string sql = "exec [dbo].[proc_Fenye] @pageSize, @keyName, @tabelName, @where , @currentPage, @rows out";
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@pageSize",fenYe.PageSize);
                parameters.Add("@keyName", "FskId");
                parameters.Add("@tabelName", "CFSK");
                parameters.Add("@where", "");
                parameters.Add("@currentPage", fenYe.CurrentPage);
                parameters.Add("@rows", direction: ParameterDirection.Output, dbType: DbType.Int32);

                List<CFSK> cFSKs =  sc.Query<CFSK>(sql, parameters).ToList();

                fenYe.list = cFSKs;
                fenYe.Rows = parameters.Get<int>("rows");

                return fenYe;
            }
        }

        public async Task<List<CFSK>> CFSKSelectid(string id)
        {
            return await myDbContext.CFSKs.Where(e => EF.Functions.Like(e.FirstKindId, $"%{id}%")).ToListAsync();
            //List<CFSK> list = await myDbContext.CFSKs.FirstOrDefaultAsync(c => c.FirstKindId == id);

        }

        /// <summary>
        /// 查询2级结构表所有数据并作为下拉框
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<CFSK>> CFSKSelectOption()
        {
            return await myDbContext.CFSKs.ToListAsync();
        }

        /// <summary>
        /// 修改2级结构表数据
        /// </summary>
        /// <param name="cFSK"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> CFSKUpdateAsync(CFSK cFSK)
        {
            myDbContext.CFSKs.Update(cFSK);
            return await myDbContext.SaveChangesAsync();
        }
    }
}
